package de.koller.hydrogen.render;

import de.koller.hydrogen.HNode;
import de.koller.hydrogen.HydrogenStart;
import de.koller.oceanFramework.math.RVec2;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class HCamera extends HNode {

	private AffineTransform backupTransform;
	public final RVec2 position = new RVec2( -HydrogenStart.width / 2, -HydrogenStart.height / 2 );
	public float scale = 2f;
	
	public void transform( Graphics2D g ) {
		backupTransform = g.getTransform();
		g.setTransform( createTransform() );
	}
	
	public AffineTransform createTransform() {
		AffineTransform a = new AffineTransform();
		a.translate( -position.x, -position.y );
		a.scale( scale, scale );
		return a;
	}
	
	public void restore( Graphics2D g ) {
		g.setTransform( backupTransform );
	}

	public void scaleCamera( float dz, int x, int y ) {
		final float scaleFactor = 1.1f;
		
		position.x += x;
		position.y += y;

		if( dz < 0 && scale < 100 ) {
			scale *= scaleFactor;
			position.x *= scaleFactor;
			position.y *= scaleFactor;
		} else if( dz > 0 && scale > 1f ) {
			scale /= scaleFactor;
			position.x /= scaleFactor;
			position.y /= scaleFactor;
		}

		position.x -= x;
		position.y -= y;
	}
	
	public void moveCameraInPixel( float dx, float dy ) {
		this.position.add( -dx, -dy );
	}
	
}
